<?php

session_start();
header("Content-Type: text/html;charset=utf-8");
require 'lyb_conn.php';
$page_size = 20; //每页显示数量
$page_now = $_POST[p]; //获取当前页，p为用户指定的当前页数
//print_r($page_now);
if (!isset($page_now)) {
    $page_now = 1;
}
$page_start = ($page_now - 1) * $page_size; //设置查询起始值
$zscx = "select count(*) as total from n1"; //查询表单总数据量
$sjcx = "select * from n1 order by add_date desc limit $page_start,$page_size"; //分页查询
$result = mysqli_query($con, $sjcx);
$zongshu = mysqli_query($con, $zscx);
$rows_count = mysqli_fetch_array($zongshu);
$rows_count = $rows_count['total']; //总数量
$page_count = ceil($rows_count / $page_size); //总页数,ceil函数返回不小于处理结果的下一个整数
$i = 1;
$i = ($page_now - $i) * $page_size; //设置翻页之后的当前页第一行的序号值
$rows = mysqli_fetch_all($result, MYSQLI_ASSOC);
$arr['page_list'] = $rows;

/* 编写分页显示 */
//$url = $_SERVER["PHP_SELF"] . "?p="; //拼接url
$url = "index.php";

/* 首页和上一页 */
if ($page_now > 1) {        //如果当前页大于第一页，则显示首页和下一页链接
    $page_banner = "<a href='javascript:setpage(1)'>首页</a>" . "&nbsp";
    $page_banner .= "<a href='javascript:setpage(" . ($page_now - 1) . ")'>上一页</a>" . "&nbsp";
} else {    //如果当前页已经是第一页，则取消首页和上一页的链接
    $page_banner = "<a disable='true'>首页</a>" . "&nbsp";
    $page_banner .= "<a disable='true'>上一页</a>" . "&nbsp";
}

/* 判断1 2 3 4 5页数的排序，理论：当前页显示在排序的第5位，向前显示4页，向后显示5页，根据当前页数循环显示左右的其他页数 */

if ($page_count <= 10) {         //判断总页数是否小于10，如果小于，则直接显示从1开始到最后页结束
    for ($j = 1; $j <= $page_count; $j++) {
        $page_banner .= "<a href='javascript:setpage(" . $j . ")'>" . $j . "</a>" . "&nbsp";
    }
} elseif ($page_now < 6) {      //如果当前页小于6且总页数大于10，，则显示1-10的页数
    for ($j = 1; $j <= 10; $j++) {
        $page_banner .= "<a href='javascript:setpage(" . $j . ")'>" . $j . "</a>" . "&nbsp";
        //$page_banner.="<a href=" . $url . $j . ">" . $j . "</a>"."&nbsp";
    }
} elseif ($page_now >= ($page_count - 4)) {     //如果当前页已在倒数1-5页之内，则直接显示最后10页，不需向后循环
    for ($j = 9; $j >= 0; $j--) {
        $page_banner .= "<a href='javascript:setpage(" . ($page_count - $j) . ")'>" . ($page_count - $j) . "</a>" . "&nbsp";
    }
} else {            // 6=<当前页<=pagecount-5，则根据当前页进行循环
    $j = $page_now;
    for ($j; $j <= $j + 5; $j++) {
        $page_banner .= "<a href='javascript:setpage(" . ($j - 4) . ")'>" . ($j - 4) . "</a>" . "&nbsp";
        $k += 1;
        if ($k >= 10) {
            break;
        }
    }
}

/* 下一页和末页 */
if ($page_now < $page_count) {  //如果当前页小于最后一页，则显示下一页和末页链接
    $page_banner .= "<a href='javascript:setpage(" . ($page_now + 1) . ")'>下一页</a>" . "&nbsp";
    $page_banner .= "<a href='javascript:setpage(" . $page_count . ")'>末页</a>" . "&nbsp";
} else {    //如果当前页已是最后一页，则取消下一页和末页的连接
    $page_banner .= "<a disable='true'>下一页</a>" . "&nbsp";
    $page_banner .= "<a disable='true'>末页</a>";
}

/*显示总记录和当前页*/
$page_banner .= "&nbsp" . "共:<span class='ly_3'>" . $rows_count . "</span>条记录," . "&nbsp";
$page_banner .= "<span class='ly_3'>" . $page_now . "/" . $page_count . "</span>页";

/* 跳页 */
$page_banner .= "&nbsp" . "<input style='width:40px; type='text' id='p1'>" . "&nbsp";
$page_banner .= "<button type='button' onclick='zd()'>转到</button>";

/*完成分页条的拼接*/
$arr['page_banner'] = $page_banner;

/*json arr数组*/
echo json_encode($arr);






/*
  echo "<table border='1' align='center' cellspacing='0' width='70%'>";
  echo "<tr>";
  echo "<td align='center'>序号</td>";
  echo "<td align='center'>用户名</td>";
  echo "<td align='center'>标题</td>";
  echo "<td align='center'>内容</td>";
  echo "<td align='center'>时间</td>";
  echo "<td align='center' colspan='2'>操作</td>";
  echo "</tr>";


  while ($rows = mysqli_fetch_array($result)) {
  $i += 1;
  echo "<tr>";
  echo "<td class='ly_1' align='center'>$i</td>";
  echo "<td class='ly_1'>$rows[user]</td>";
  echo "<td class='ly_1'>$rows[title]</td>";
  echo "<td class='ly_1'>$rows[content]</td>";
  echo "<td class='ly_1'>$rows[add_date]</td>";
  $_SESSION['url'] = $_SERVER["REQUEST_URI"];
  if ($_SESSION['user'] == 'admin') {
  echo "<td class='ly_2'><a href='lyb_edit.php?id=$rows[id]'>编辑</a></td>";
  echo "<td class='ly_2'><a href='lyb_delete.php?id=$rows[id]'>删除</a></td>";
  } elseif ($_SESSION['user'] == "") {
  echo "<td class='ly_3'>登录之后更多权限</td>";
  } else {
  if ($rows[user] == $_SESSION['user']) {
  echo "<td class='ly_2'><a href='lyb_edit.php?id=$rows[id]'>编辑</a></td>";
  echo "<td class='ly_2'><a href='lyb_delete.php?id=$rows[id]'>删除</a></td>";
  } else {
  echo "<td class='ly_3' colspan='2'>无权限</td>";
  }
  }
  echo "</tr>";
  }
  echo "</table>";
 * 
 */
?>


